package csu.big_article.controller;

import csu.big_article.pojo.Article;
import csu.big_article.pojo.PageBean;
import csu.big_article.pojo.Result;
import csu.big_article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public Result list() {
        return Result.success("get list from article");
    }

    @PostMapping("/article")
    public Result Publish(@RequestBody @Validated Article article) {
        articleService.Publish(article);
        return Result.success();
    }

    @PutMapping("/article")
    public Result Update(@RequestBody @Validated Article article) {
        articleService.Update(article);
        return Result.success();
    }

    @DeleteMapping("/article")
    public Result Delete(@RequestParam Integer id) {
        articleService.Delete(id);
        return Result.success();
    }

    @GetMapping("/article")
    @CrossOrigin(origins = "http://localhost:5173", maxAge = 3600)
    public Result <PageBean<Article>> GetList(Integer pageNum,
                                              Integer pageSize,
                                              @RequestParam(required = false) Integer categoryId,
                                              @RequestParam(required = false) String state) {
        //这里的分页查询使用mybatis提供的PageHelper
        PageBean<Article> pb = articleService.GetList(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    //获取文章详细信息
    @GetMapping("/article/detail")
    public Result<Article> getDetailById(@RequestParam Integer id) {
        Article article = articleService.getDetailById(id);
        return Result.success(article);
    }




}
